package com.chd.driversystem.util.network;

import com.chd.driversystem.util.network.tools.HttpConstants;
import com.chd.driversystem.util.network.bean.Param;
import com.chd.driversystem.util.tools.MyApplication;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by shawnfly on 17-5-2.
 */

public class HttpAccess {
    //请求连接超时时间
    public static final long CONNECT_TIMEOUT = 15L;
    //请求读取超时时间
    public static final long READ_TIMEOUT = 20L;
    //请求写超时时间
    public static final long WRITE_TIMEOUT = 20L;
    //实例
    private static HttpAccess mInstance;
    //OkHttpClient对象
    private static OkHttpClient mOKHttpClient;

    private Retrofit retrofit;

    private HttpRetrofitAccess retrofitAccess;

    public HttpRetrofitAccess getRetrofitAccess() {
        return retrofitAccess;
    }

    private HttpAccess() {
        File sdcache = MyApplication.getContext().getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        mOKHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(HttpConstants.LOGIN_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofitAccess = retrofit.create(HttpRetrofitAccess.class);
    }

    public static HttpAccess getmInstance() {
        if (mInstance == null) {
            synchronized (HttpAccess.class) {
                if (mInstance == null) {
                    mInstance = new HttpAccess();
                }
            }
        }
        return mInstance;
    }
}
